//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006
//
// Show/hide attached file with raster, tracks, waypoints or routes.
//

# include "StdAfx.h"
# include "UndoRedo.h"
# include "ShowHideAttachment.h"
# include "Map.h"
# include "Selection.h"
# include "Globals.h"

class CShowAttachmentAction : public CEditAction {
  public:
	CShowAttachmentAction (file_ref_t * _pFileRef, bool _bVisible, bool _bRenderer) :
		m_strFileName (_pFileRef->strFileName),
		m_pFileRef (_pFileRef),
		m_bVisible (_bVisible),
		m_bRenderer (_bRenderer)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CShowAttachmentAction::Do ();}

  private:
	const string_t m_strFileName;
	file_ref_t * const m_pFileRef;
	const bool m_bVisible;
	const bool m_bRenderer;
};

void CShowAttachmentAction::GetName (string_t & _str) const {
	CCritSec::CLock lock (g_cs);

	_str.append (m_bVisible ? "show [" : "hide [");
	_str.append (m_strFileName);
	_str.append ("]");
}

bool CShowAttachmentAction::Do () {
	CCritSec::CLock lock (g_cs);

	assert (m_pFileRef);
	m_pFileRef->bVisible = m_bVisible;

	if (m_bRenderer)
		UpdateOverlays (g_map.renderers);

	return true;
}

void CShowAttachmentAction::Undo () {
	CCritSec::CLock lock (g_cs);

	assert (m_pFileRef);
	m_pFileRef->bVisible = ! m_bVisible;

	if (m_bRenderer)
		UpdateOverlays (g_map.renderers);
}

////////////////////////////////////////////

void ShowAttachment (file_ref_t * _pFileRef, bool _bVisible, bool _bRenderer) {
	PerformEditAction (new CShowAttachmentAction (_pFileRef, _bVisible, _bRenderer));
}
